<?php
namespace app\index\model;

use think\Model;
use think\Db;

class BaseModel extends Model
{

    protected $tb_position = 'pract_position';
    protected $tb_setting = 'pract_setting';
    protected $tb_finance = 'pract_finance';
    protected $tb_log = 'pract_log';
    
    /**
     * Undocumented function
     *
     * @param string $uid 用户id
     * 
     * @return void
     */
    public function bulidPurse($uid)
    {
        $data = [
            'uid' => $uid
        ];
        $row = Db::table($this->tb_finance)
                ->insert($data);
        if ($row != 1) {
            return false;
        }
        return true;
    }

    /**
     * 用户钱包
     * 
     * @param string $uid 用户id
     *
     * @return array
     */
    public function purse($uid)
    {
        if($uid > 0)
        {

            $where = [
                'uid' => $uid,
            ];
            $row = Db::table($this->tb_finance)
            ->where($where)
            ->count();
            if($row == 0)
            {
                $data =
                [
                    'uid'=>$uid,
                ];
                Db::table($this->tb_finance)
                ->insert($data);
            }
            return Db::table($this->tb_finance)
            ->where($where)
            ->find();
        }
        else
        {
            return false;
        }
    }
    
    
    public function getStage()
    {
        $stage = 
        [
            ['sid'=>0,'name'=>'全部'],
            ['sid'=>1,'name'=>'小学'],
            ['sid'=>2,'name'=>'初中'],
            ['sid'=>3,'name'=>'高中']
        ];
        return $stage;
    }
    
    public function city($city,$iscity=0)
    {
        $where=
        [
            ['name','like','%'.$city.'%']
        ];
        if($iscity != 0)
        {
            $where['pcode']=['pcode','<>',0];
        }
        return  Db::table($this->tb_position)
                ->where($where)
                ->find();
    }
    public function cityByCode($city_code)
    {
        $where=
        [
            ['code','=',$city_code]
        ];
        return  Db::table($this->tb_position)
                ->where($where)
                ->find();
    }
    
    public function cityByName($city_code,$city)
    {
        $where=
        [
            ['pcode','=',$city_code],           
            ['name','like','%'.$city.'%']
        ];
        return  Db::table($this->tb_position)
                ->where($where)
                ->find();
    }

    public function settingInfo($type=0)
    {
        if($type === 0)
        {
            
            $where=[];
        }
        else
        {
            $where=
            [
                ['set_type','=',$type]
            ];
        }
        return  Db::table($this->tb_setting)
                ->where($where)
                ->select();
    }
    public function insertLog($title,$content)
    {
        $data=
        [
            'title'=>$title,
            'content'=>$content,
        ];
        return  Db::table($this->tb_log)                
                    ->insert($data);
    }
}